home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12919 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: dante.network-1.com!garrett
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Help: Containers for templated classes
  4. Message-ID: <1996Mar22.020605.6@network-1.com>
  5. From: garrett@I_should_put_my_domain_in_etc_NNTP_INEWS_DOMAIN (Don Garrett)
  6. Date: 22 Mar 96 02:06:01 -0600
  7. References: <314740D4.23B1@mit.edu>
  8. Organization: Network-1
  9. Nntp-Posting-Host: dante.network-1.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Imran Haq (ihaq@mit.edu) wrote:
  13. : I'm kind of new to advanced usage of templates. Does anyone know how 
  14. : to put different template classes in a single container. Is this possible?
  15. : For example, I have instations
  16.  
  17. :   myClass<int>   *type1;
  18. :   myClass<double>   *type;
  19.  
  20. :  ....
  21.  
  22. : I'm using STL to provide container classes:
  23.  
  24. :   vector<myClass<????> >  container;
  25.  
  26. : But this obviously does not work. Does anyone know about any
  27. : workarounds?? HELP!!!
  28.  
  29.  
  30. : Imran Haq
  31. : ihaq@mit.edu
  32.  
  33.   Of course it depends on what you are wanting to do, but here is a
  34. solution.
  35.  
  36. class myBase;
  37.  
  38. template myClass<T> : public myBase {}
  39.  
  40.   Then use a container that holds POINTERS to myBase. Use virtual
  41. functions to make sure that appropriate type variant functions are
  42. used on myClass.
  43.  
  44.   The reason you can't put myClass<int> and myClass<double> in the
  45. same vector is that they are different types. They must be different
  46. because the interfaces to these classes (and even the size of an
  47. instance of the class) can vary. By using myBase, the interface
  48. (and the type) don't vary.
  49.  
  50. --
  51. Don Garrett                               http://www.network-1.com/~garrett/
  52. garrett@network-1.com
  53.